﻿namespace LeetCodeProject._0001_1000._0002;

// 两数相加
public class Solution
{
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
    {
        if (l1 == null)
            return l2;
        if (l2 == null)
            return l1;
        // 虚拟头节点
        ListNode dummyHead = new ListNode(0);
        // 进位值
        int carry = 0;
        ListNode last = dummyHead;
        while (l1 != null || l2 != null)
        {
            int v1 = 0;
            if (l1 != null)
            {
                v1 = l1.val;
                l1 = l1.next;
            }

            int v2 = 0;
            if (l2 != null)
            {
                v2 = l2.val;
                l2 = l2.next;
            }

            int sum = v1 + v2 + carry;
            carry = sum / 10;
            // sum的个位数做为新节点的值
            last.next = new ListNode(sum % 10);
            last = last.next;
        }

        if (carry > 0)
        {
            last.next = new ListNode(carry);
        }

        return dummyHead.next;
    }
}

public class ListNode
{
    public int val;
    public ListNode next;

    public ListNode(int val = 0, ListNode next = null)
    {
        this.val = val;
        this.next = next;
    }
}